using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GunAwardV2 : EnemyBaseFixed
{
    [System.Serializable]
    public class Config
    {
        public Sprite sprite;
        public float intervalTime = 1f;
        public bool interactive = false;
    }

    [Header("奖励配置")]
    public ABullet.BulletType bulletType = ABullet.BulletType.M;
    public Config[] configSequence;
    public GameObject gunPrefab;

    private float lastTime;
    private int index = 0;
    private SpriteRenderer sr;
    private Collider2D collider2d;

    private void Awake()
    {
        base.Init();
        sr = GetComponent<SpriteRenderer>();
        collider2d = GetComponent<Collider2D>();
    }
    void Start()
    {
        health = 0;
        lastTime = Time.time;
        Run();
    }

    // Update is called once per frame

    private void FixedUpdate()
    {
        UpdateWorkingSwitch();
        if (running)
        {
            base.CommonFixedUpdate();
        }


    }
    private void UpdateWorkingSwitch()
    {
        if (Utility.InCameraViewport(Camera.main, transform.position + (Vector3.right * 1.0f))
            && Utility.InCameraViewport(Camera.main, transform.position + (Vector3.left * 1.0f)))
        {
            StartWork();
        }
        else
        {
            StopWork();
        }
    }

    protected virtual void UpdateStateNone(bool stateChanged, EnumState inState, out EnumState outState)
    {
        outState = inState;

        index = 0;
        Config config = configSequence[index];
        sr.sprite = config.sprite;
        collider2d.enabled = false;

    }
    protected virtual void UpdateStateWorking(bool stateChanged, EnumState inState, out EnumState outState)
    {
        outState = inState;

        Config config = configSequence[index];
        sr.sprite = config.sprite;
        float t = Time.time - lastTime;
        if (t >= config.intervalTime)
        {
            lastTime = Time.time;
            index++;
            if (index >= configSequence.Length)
            {
                index = 0;
            }
        }
        collider2d.enabled = config.interactive;
    }


    public void StartWork()
    {
        state = EnumState.Working;
    }
    public void StopWork()
    {
        state = EnumState.None;
    }

    //public override float Damage(GameObject source, float value)
    //{
    //    Die();
    //    return 0;
    //}

    //public override void Die()
    //{
    //    ThrowGun();
    //    Boom();
    //}

    protected override void Die(EnumState inState, out EnumState outState)
    {
        outState = inState;
        if (running)
        {
            outState = EnumState.Die;
            ThrowGun();
            Boom();
        }
    }
    private void Boom()
    {
        if (boomPrefab != null)
        {
            Instantiate(boomPrefab, transform.position, Quaternion.identity);
            Constant.Sounds.EnemyDeath1.PlayOneShot();
        }
        Destroy(gameObject);
    }
    private void ThrowGun()
    {
        if (gunPrefab != null)
        {
            GameObject gunObj = Instantiate(gunPrefab, transform.position, Quaternion.identity);
            if (gunObj != null)
            {
                Gun gun = gunObj.GetComponent<Gun>();
                gun.SetBulletType(bulletType);
                gun.Throw();
            }
        }
    }

}
